package com.anzia.study_2.slice;

import com.anzia.study_2.ResourceTable;
import com.anzia.study_2.utils.DeviceUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.bundle.ElementName;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

	private Button btn1;
	private Button btn2;
	private Button btn3;

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		// 通过组件id获得组件
		btn1 = (Button)findComponentById(ResourceTable.Id_migration_btn_01);
		// 设置按钮的点击监听事件
		btn1.setClickedListener(component -> {
			// 要实现跨设备的打开FA
			// 第一步当然就是要获取到设备id
			String deviceId = DeviceUtils.getDeviceId();

			if (deviceId != null) {
				// 新建Intent，意图：信息的载体
				Intent btn_1_intent = new Intent();
				// OperationBuilder()方式
				Operation operation = new Intent.OperationBuilder()
						.withDeviceId(deviceId) // 获得设备id，在本地的时候可以为空
						.withBundleName("com.anzia.study_2")	// 包名，在config.json中可找到
						.withAbilityName("com.anzia.study_2.RemoteAbility") // 设立目标页面，一定要路径名！！！
						.withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)	// 设立标签，允许跨设备
						.build();	// 构建

				btn_1_intent.setOperation(operation);	// 把operation存进intent中

				startAbility(btn_1_intent);
			}

		});

		btn2 = (Button)findComponentById(ResourceTable.Id_migration_btn_02);
		btn2.setClickedListener(component -> {
			Intent btn_2_intent = new Intent();
			ElementName migrationSliceEn = new ElementName("", "com.anzia.study_2", "MigrationAbility");
			btn_2_intent.setElement(migrationSliceEn);
			startAbility(btn_2_intent);
		});

		btn3 = (Button)findComponentById(ResourceTable.Id_migration_btn_03);
		// 点击按钮3，跳转到MigrationBackAbility
		btn3.setClickedListener(component -> {
			Intent btn_3_intent = new Intent();
			ElementName migrationBackSliceEn = new ElementName("", "com.anzia.study_2", "MigrationBackAbility");
			btn_3_intent.setElement(migrationBackSliceEn);
			startAbility(btn_3_intent);
		});
	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}
}
